package com.sq.auth.application.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.sq.auth.application.dto.AuthPermissionDTO;
import com.sq.auth.application.dto.AuthRolePermissionDTO;
import com.sq.auth.common.entity.Result;
import com.sq.auth.domain.bo.AuthPermissionBO;
import com.sq.auth.domain.bo.AuthRolePermissionBO;
import com.sq.auth.domain.service.AuthPermissionDomainService;
import com.sq.auth.domain.service.AuthRolePermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author quan
 */
@RestController
@RequestMapping("/rolePermission/")
@Slf4j
public class AuthRolePermissionController {
    @Resource
    private AuthRolePermissionDomainService authRolePermissionDomainService;

    @PostMapping("add")
    public Result add(@RequestBody AuthRolePermissionDTO authRolePermission) {
        if (log.isInfoEnabled()) {
            log.info("AuthRolePermissionController.add.dto:{}", JSON.toJSONString(authRolePermission));
        }
        try {
            Preconditions.checkNotNull(authRolePermission.getRoleId(), "角色id不能为空");
            Preconditions.checkArgument(CollUtil.isNotEmpty(authRolePermission.getPermissionIdList()), "权限集合不能为空");
            AuthRolePermissionBO authRolePermissionBO =   BeanUtil.copyProperties(authRolePermission, AuthRolePermissionBO.class);
            return Result.ok(authRolePermissionDomainService.add(authRolePermissionBO));
        } catch (Exception e) {
            log.error("AuthRolePermissionController.add.error:{}", e.getMessage());
            return Result.fail("新增角色权限失败");
        }
    }

}
